<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Netty自定义协议编解码深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .code-dots {
            display: flex;
            gap: 8px;
        }
        
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            line-height: 1.6;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            margin-bottom: 2rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 2px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 2rem;
            border-radius: 16px;
            position: relative;
            overflow: hidden;
            transition: all 0.3s ease;
        }
        
        .feature-card::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            transform: rotate(45deg);
        }
        
        .feature-card:hover {
            transform: scale(1.05);
            box-shadow: 0 15px 30px rgba(0,0,0,0.2);
        }
        
        .protocol-visual {
            background: #f8f9fa;
            border: 2px solid #e9ecef;
            border-radius: 12px;
            padding: 2rem;
            margin: 2rem 0;
        }
        
        .protocol-part {
            display: inline-block;
            padding: 0.5rem 1rem;
            margin: 0.25rem;
            border-radius: 8px;
            font-weight: 600;
            transition: all 0.3s ease;
        }
        
        .protocol-header {
            background: #e3f2fd;
            color: #1976d2;
            border: 2px solid #1976d2;
        }
        
        .protocol-content {
            background: #f3e5f5;
            color: #7b1fa2;
            border: 2px solid #7b1fa2;
        }
        
        .protocol-part:hover {
            transform: scale(1.1);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        .mermaid {
            margin: 2rem 0;
            text-align: center;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
            border-left: 4px solid #ff6b6b;
            padding: 1.5rem;
            border-radius: 8px;
            margin: 1.5rem 0;
        }
        
        .step-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border-radius: 50%;
            font-weight: bold;
            margin-right: 1rem;
            flex-shrink: 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-network-wired mr-4"></i>
                Netty自定义协议编解码
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入探索网络通信的核心技术，掌握高性能协议设计的艺术
            </p>
            <div class="flex justify-center space-x-6 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-code mr-2"></i>
                    <span>灵活编码</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-exchange-alt mr-2"></i>
                    <span>高效传输</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-shield-alt mr-2"></i>
                    <span>安全可靠</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <div class="prose max-w-none">
                <p class="text-lg leading-relaxed text-gray-700 indent-8">
                    在Netty中，自定义协议的编码和解码涉及到对数据的格式和内容进行自定义处理。Netty提供了灵活的机制来实现自定义协议的编码和解码，通过实现特定的<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">Codec</code>类来完成。
                </p>
            </div>
        </div>

        <!-- Protocol Definition Section -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <span class="step-number">1</span>
                定义自定义协议
            </h2>
            
            <p class="text-lg text-gray-700 mb-6 indent-8">
                首先，需要明确自定义协议的格式和内容。例如，假设我们有一个简单的协议，其中每个消息包含一个固定长度的头部和一个可变长度的内容。头部包括消息的长度和类型，内容是实际传输的数据。
            </p>

            <div class="protocol-visual">
                <h3 class="text-xl font-semibold mb-4 text-gray-800">
                    <i class="fas fa-cube mr-2 text-purple-600"></i>
                    协议结构示例
                </h3>
                <div class="flex flex-wrap justify-center items-center">
                    <div class="protocol-part protocol-header">
                        <i class="fas fa-ruler mr-2"></i>Length (4字节)
                    </div>
                    <div class="protocol-part protocol-header">
                        <i class="fas fa-tag mr-2"></i>Type (1字节)
                    </div>
                    <div class="protocol-part protocol-content">
                        <i class="fas fa-file-alt mr-2"></i>Content (可变长度)
                    </div>
                </div>
            </div>

            <!-- Mermaid Diagram -->
            <div class="mermaid">
                graph LR
                    A[原始数据] -->|编码| B[Header + Content]
                    B -->|网络传输| C[字节流]
                    C -->|解码| D[还原数据]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style B fill:#bbf,stroke:#333,stroke-width:2px
                    style C fill:#bfb,stroke:#333,stroke-width:2px
                    style D fill:#fbf,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Encoder Section -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <span class="step-number">2</span>
                实现自定义编码器（Encoder）
            </h2>
            
            <p class="text-lg text-gray-700 mb-6 indent-8">
                编码器负责将自定义协议的对象转换为字节流。Netty提供了<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">MessageToByteEncoder</code>类，可以用于实现自定义的编码器。
            </p>

            <div class="code-block">
                <div class="code-header">
                    <div class="code-dots">
                        <div class="code-dot dot-red"></div>
                        <div class="code-dot dot-yellow"></div>
                        <div class="code-dot dot-green"></div>
                    </div>
                    <span>CustomProtocolEncoder.java</span>
                </div>
                <pre><code>import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class CustomProtocolEncoder extends MessageToByteEncoder&lt;CustomMessage&gt; {

    @Override
    protected void encode(ChannelHandlerContext ctx, CustomMessage msg, ByteBuf out) throws Exception {
        // 写入消息长度（4字节）
        out.writeInt(msg.getLength());
        
        // 写入消息类型（1字节）
        out.writeByte(msg.getType());
        
        // 写入消息内容
        out.writeBytes(msg.getContent());
    }
}</code></pre>
            </div>

            <div class="highlight-box mt-6">
                <p class="text-gray-800">
                    <i class="fas fa-lightbulb mr-2 text-orange-600"></i>
                    <strong>编码器要点：</strong>在编码器中，<code class="bg-orange-100 text-orange-800 px-2 py-1 rounded">CustomMessage</code>是自定义的消息对象，包含了<code class="bg-orange-100 text-orange-800 px-2 py-1 rounded">getLength()</code>、<code class="bg-orange-100 text-orange-800 px-2 py-1 rounded">getType()</code>和<code class="bg-orange-100 text-orange-800 px-2 py-1 rounded">getContent()</code>方法，用于获取消息的长度、类型和内容。
                </p>
            </div>
        </div>

        <!-- Decoder Section -->
        <div class="content-card rounded-2xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-6">
                <span class="step-number">3</span>
                实现自定义解码器（Decoder）
            </h2>
            
            <p class="text